from selenium.webdriver.common.by import By
from time import sleep, strftime
from ui_auto.public.pages.basepage import BasePage
from ui_auto.data.page_elem import Analysysdata_elem
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver import ActionChains

class Create_webhook(BasePage):
    def test_webhook(self):
        ea=BasePage.get_driver()
        action_chains = ActionChains(ea)
        now = strftime("%Y-%m-%d-%H-%M-%S")
        #进入wf页面
        BasePage.click(BasePage.search_elem(['XPATH', Analysysdata_elem.wf_list]))
        #进入创建wf页面
        WebDriverWait(ea, 5).until(lambda _: ea.find_element(By.XPATH, Analysysdata_elem.create_wf_btn))
        BasePage.click(BasePage.search_elem(['XPATH', Analysysdata_elem.create_wf_btn]))

        WebDriverWait(ea, 5).until(lambda _: ea.find_element(By.XPATH, Analysysdata_elem.cancel_wf_btn))
        BasePage.click(BasePage.search_elem(['XPATH', Analysysdata_elem.cancel_wf_btn]))
        #输入wf名称
        BasePage.sendkeys(BasePage.search_elem(['XPATH', Analysysdata_elem.input_wf_name]), "test_wf_" + now)
        #拖拽分群组件
        action_chains.move_to_element(BasePage.search_elem(['XPATH', Analysysdata_elem.fangzhoufenqun]))
        action_chains.click_and_hold()
        action_chains.move_by_offset(260,0)
        action_chains.release()
        action_chains.perform()
        #选择分群
        BasePage.click(BasePage.search_elem(['XPATH', Analysysdata_elem.click_edit]))
        WebDriverWait(ea, 5).until(lambda _: ea.find_element(By.XPATH, Analysysdata_elem.target_customer_text))
        BasePage.click(BasePage.search_elem(['XPATH', Analysysdata_elem.select_target_customer_segmention]))
        BasePage.sendkeys(BasePage.search_elem(['XPATH', Analysysdata_elem.search_customer_segmention]), "tianli")
        BasePage.click(BasePage.search_elem(['XPATH', Analysysdata_elem.target_customer]))
        BasePage.click(BasePage.search_elem(['XPATH', Analysysdata_elem.OK_btn]))
        #添加条件
        WebDriverWait(ea, 5).until(lambda _: ea.find_element(By.XPATH, Analysysdata_elem.clustering_component))
        action_chains.move_to_element(BasePage.search_elem(['XPATH', Analysysdata_elem.clustering_component]))
        action_chains.move_to_element(BasePage.search_elem(['XPATH', Analysysdata_elem.add_step]))
        action_chains.click_and_hold()
        action_chains.move_by_offset(50, 0)
        action_chains.release()
        action_chains.perform()
        #选择人群条件
        BasePage.click(BasePage.search_elem(['XPATH', Analysysdata_elem.user_condition]))
        BasePage.click(BasePage.search_elem(['XPATH', Analysysdata_elem.click_edit]))
        BasePage.click(BasePage.search_elem(['XPATH', Analysysdata_elem.user_properties_select]))
        BasePage.sendkeys(BasePage.search_elem(['XPATH', Analysysdata_elem.search_user_properties]), "手机号")
        BasePage.click(BasePage.search_elem(['XPATH', Analysysdata_elem.phone_property]))
        BasePage.sendkeys(BasePage.search_elem(['XPATH', Analysysdata_elem.input_value]), "15174304291")
        BasePage.click(BasePage.search_elem(['XPATH', Analysysdata_elem.OK_btn]))
        #是，选择webhook通道
        WebDriverWait(ea, 5).until(lambda _: ea.find_element(By.XPATH, Analysysdata_elem.user_component))
        action_chains.move_to_element(BasePage.search_elem(['XPATH', Analysysdata_elem.user_component]))
        action_chains.move_to_element(BasePage.search_elem(['XPATH', Analysysdata_elem.yes_btn]))
        action_chains.click_and_hold()
        action_chains.move_by_offset(50, 0)
        action_chains.release()
        action_chains.perform()
        WebDriverWait(ea, 5).until(lambda _: ea.find_element(By.XPATH, Analysysdata_elem.webhook_channel))
        BasePage.click(BasePage.search_elem(['XPATH', Analysysdata_elem.webhook_channel]))
        BasePage.click(BasePage.search_elem(['XPATH', Analysysdata_elem.click_edit]))
        BasePage.click(BasePage.search_elem(['XPATH', Analysysdata_elem.select_third_party_system]))
        # WebDriverWait(ea, 5).until(lambda _: ea.find_element(By.XPATH, Analysysdata_elem.select_api))
        sleep(1)
        BasePage.click(BasePage.search_elem(['XPATH', Analysysdata_elem.select_api]))
        BasePage.click(BasePage.search_elem(['XPATH', Analysysdata_elem.select_connect]))
        # WebDriverWait(ea, 5).until(lambda _: ea.find_element(By.XPATH, Analysysdata_elem.select_ea_api))
        sleep(1)
        BasePage.click(BasePage.search_elem(['XPATH', Analysysdata_elem.select_ea_api]))
        BasePage.click(BasePage.search_elem(['XPATH', Analysysdata_elem.OK_btn]))
        #选择时间
        BasePage.click(BasePage.search_elem(['XPATH', Analysysdata_elem.time_range]))
        sleep(1)
        BasePage.click(BasePage.search_elem(['XPATH', Analysysdata_elem.end_time("2024-3-20")]))
        BasePage.click(BasePage.search_elem(['XPATH', Analysysdata_elem.end_time('2024-3-21')]))
        BasePage.click(BasePage.search_elem(['XPATH', Analysysdata_elem.release_wf]))




